<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: mslaria $ ($Date: 2007/07/17 13:37:17 $)
// $RCSfile: functions_contactsrender.php,v $ : $Revision: 1.6 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

/**
* Renders the Contact Cards
*/
function renderContactCards($cardtype, $searchquery = false)
{
	global $dbCore, $_SWIFT;

	// First load the contacts
	if (empty($searchquery))
	{
		// Load all Contacts
		$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."contacts` WHERE (`contacttype` = '". SWIFTPUBLIC ."') OR (`contacttype` = '". SWIFTPRIVATE ."' AND `staffid` = '". intval($_SWIFT["staff"]["staffid"]) ."') ORDER BY `fullname` ASC;");
	} else {
		if (strlen($searchquery) <= 2)
		{
			$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."contacts` WHERE ((`contacttype` = '". SWIFTPUBLIC ."') OR (`contacttype` = '". SWIFTPRIVATE ."' AND `staffid` = '". intval($_SWIFT["staff"]["staffid"]) ."')) AND `fullname` LIKE '". $dbCore->escape($searchquery) ."%' ORDER BY `fullname` ASC;");
		} else {
			$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."contacts` WHERE ((`contacttype` = '". SWIFTPUBLIC ."') OR (`contacttype` = '". SWIFTPRIVATE ."' AND `staffid` = '". intval($_SWIFT["staff"]["staffid"]) ."')) AND (`fullname` LIKE '%". $dbCore->escape($searchquery) ."%' OR `email1address` LIKE '%". $dbCore->escape($searchquery) ."%' OR `email2address` LIKE '%". $dbCore->escape($searchquery) ."%' OR `email3address` LIKE '%". $dbCore->escape($searchquery) ."%' OR `companyname` LIKE '%". $dbCore->escape($searchquery) ."%' OR `businessaddress` LIKE '%". $dbCore->escape($searchquery) ."%' OR `homeaddress` LIKE '%". $dbCore->escape($searchquery) ."%') ORDER BY `fullname` ASC;");
		}
	}

	$_contactlist = array();
	while ($dbCore->nextRecord())
	{
		$_contactlist[] = $dbCore->Record;
	}

	$contactcount = count($_contactlist);
	// We will have three columns, so decide on how many contacts we are gonna display on per column
	$percolumn = ceil($contactcount/3);

	$contactcount = 0;
	$column = 0;
	$_columnstore = array();
	foreach ($_contactlist as $key=>$val)
	{
		if ($contactcount >= $percolumn)
		{
			$column++;
			$contactcount = 0;
		}

		unset($coldata);

		$coldata .= '<table border="0" cellpadding="0" cellspacing="0" width="100%" style="CURSOR: pointer;" class="calborder" onMouseOver="javascript:this.className=\'calborderhover\';" onMouseOut="javascript:this.className=\'calborder\';" onClick="javascript:document.location.href=\'index.php?_m=teamwork&_a=editcontact&contactid='. intval($val["contactid"]) .'\';">';
		$coldata .= '<tr class="calinactivehour"><td style="PADDING: 3px;" class="calhrbg"><span class="tabletitle"><img src="'. $_SWIFT["themepath"] .iif($val["contacttype"]==SWIFTPUBLIC, 'icon_contacts', 'icon_contactprivate') .'.gif" border="0" align="absmiddle" />&nbsp;'. htmlspecialchars($val["fullname"]) .'</span></td></tr>';

		if ($cardtype == CONTACTVIEW_ADDRESSCARDS)
		{
			if (!empty($val["businessaddress"]))
			{
				$coldata .= '<tr class="calinactivehour"><td><table border="0" cellpadding="3" cellspacing="0" width="100%"><tr><td align="left">'. nl2br(htmlspecialchars($val["businessaddress"])) .'</td></tr></table>';
			} else if (!empty($val["homeaddress"])) {
				$coldata .= '<tr class="calinactivehour"><td><table border="0" cellpadding="3" cellspacing="0" width="100%"><tr><td align="left">'. nl2br(htmlspecialchars($val["homeaddress"])) .'</td></tr></table>';
			}

			if (!empty($val["businesstelephonenumber"]))
			{
				$coldata .= '<tr class="calinactivehour"><td><table border="0" cellpadding="3" cellspacing="0" width="100%"><tr><td align="left" width="50%" nowrap>'. $_SWIFT["language"]["clbusiness"] .'</td><td align="right">'. htmlspecialchars($val["businesstelephonenumber"]) .'</td></tr></table>';
			}

			if (!empty($val["hometelephonenumber"]))
			{
				$coldata .= '<tr class="calinactivehour"><td><table border="0" cellpadding="3" cellspacing="0" width="100%"><tr><td align="left" width="50%" nowrap>'. $_SWIFT["language"]["clhome"] .'</td><td align="right">'. htmlspecialchars($val["hometelephonenumber"]) .'</td></tr></table>';
			}

			if (!empty($val["mobiletelephonenumber"]))
			{
				$coldata .= '<tr class="calinactivehour"><td><table border="0" cellpadding="3" cellspacing="0" width="100%"><tr><td align="left" width="50%" nowrap>'. $_SWIFT["language"]["clmobile"] .'</td><td align="right">'. htmlspecialchars($val["mobiletelephonenumber"]) .'</td></tr></table>';
			}

			if (!empty($val["email1address"]))
			{
				$coldata .= '<tr class="calinactivehour"><td><table border="0" cellpadding="3" cellspacing="0" width="100%"><tr><td align="left" width="50%" nowrap>'. $_SWIFT["language"]["clemail"] .'</td><td align="right">'. htmlspecialchars($val["email1address"]) .'</td></tr></table>';
			}

		} else if ($cardtype == CONTACTVIEW_DETAILEDCARDS) {
			if (!empty($val["customerid"]))
			{
				$coldata .= '<tr class="calinactivehour"><td><table border="0" cellpadding="3" cellspacing="0" width="100%"><tr><td align="left" width="50%" nowrap>'. $_SWIFT["language"]["clcustomerid"] .'</td><td align="right">'. htmlspecialchars($val["customerid"]) .'</td></tr></table>';
			}

			if (!empty($val["email1address"]))
			{
				$coldata .= '<tr class="calinactivehour"><td><table border="0" cellpadding="3" cellspacing="0" width="100%"><tr><td align="left" width="50%" nowrap>'. $_SWIFT["language"]["clemail"] .'</td><td align="right">'. htmlspecialchars($val["email1address"]) .'</td></tr></table>';
			}

			if (!empty($val["companyname"]))
			{
				$coldata .= '<tr class="calinactivehour"><td><table border="0" cellpadding="3" cellspacing="0" width="100%"><tr><td align="left" width="50%" nowrap>'. $_SWIFT["language"]["clcompany"] .'</td><td align="right">'. htmlspecialchars($val["companyname"]) .'</td></tr></table>';
			}

			if (!empty($val["department"]))
			{
				$coldata .= '<tr class="calinactivehour"><td><table border="0" cellpadding="3" cellspacing="0" width="100%"><tr><td align="left" width="50%" nowrap>'. $_SWIFT["language"]["cldepartment"] .'</td><td align="right">'. htmlspecialchars($val["department"]) .'</td></tr></table>';
			}

			if (!empty($val["businessaddress"]))
			{
				$coldata .= '<tr class="calinactivehour"><td><table border="0" cellpadding="3" cellspacing="0" width="100%"><tr><td align="left">'.$_SWIFT["language"]["clbusiness"]."<BR />". nl2br(htmlspecialchars($val["businessaddress"])) .'</td></tr></table>';
			}
			
			if (!empty($val["homeaddress"])) {
				$coldata .= '<tr class="calinactivehour"><td><table border="0" cellpadding="3" cellspacing="0" width="100%"><tr><td align="left">'.$_SWIFT["language"]["clhome"]."<BR />". nl2br(htmlspecialchars($val["homeaddress"])) .'</td></tr></table>';
			}

			if (!empty($val["mailingaddress"]))
			{
				$coldata .= '<tr class="calinactivehour"><td><table border="0" cellpadding="3" cellspacing="0" width="100%"><tr><td align="left">'.$_SWIFT["language"]["clmailing"]."<BR />". nl2br(htmlspecialchars($val["mailingaddress"])) .'</td></tr></table>';
			}

			if (!empty($val["otheraddress"]))
			{
				$coldata .= '<tr class="calinactivehour"><td><table border="0" cellpadding="3" cellspacing="0" width="100%"><tr><td align="left">'.$_SWIFT["language"]["clother"]."<BR />". nl2br(htmlspecialchars($val["otheraddress"])) .'</td></tr></table>';
			}

			if (!empty($val["businesstelephonenumber"]))
			{
				$coldata .= '<tr class="calinactivehour"><td><table border="0" cellpadding="3" cellspacing="0" width="100%"><tr><td align="left" width="50%" nowrap>'. $_SWIFT["language"]["clbusiness"] .'</td><td align="right">'. htmlspecialchars($val["businesstelephonenumber"]) .'</td></tr></table>';
			}

			if (!empty($val["business2telephonenumber"]))
			{
				$coldata .= '<tr class="calinactivehour"><td><table border="0" cellpadding="3" cellspacing="0" width="100%"><tr><td align="left" width="50%" nowrap>'. $_SWIFT["language"]["clbusiness2"] .'</td><td align="right">'. htmlspecialchars($val["business2telephonenumber"]) .'</td></tr></table>';
			}

			if (!empty($val["hometelephonenumber"]))
			{
				$coldata .= '<tr class="calinactivehour"><td><table border="0" cellpadding="3" cellspacing="0" width="100%"><tr><td align="left" width="50%" nowrap>'. $_SWIFT["language"]["clhome"] .'</td><td align="right">'. htmlspecialchars($val["hometelephonenumber"]) .'</td></tr></table>';
			}

			if (!empty($val["home2telephonenumber"]))
			{
				$coldata .= '<tr class="calinactivehour"><td><table border="0" cellpadding="3" cellspacing="0" width="100%"><tr><td align="left" width="50%" nowrap>'. $_SWIFT["language"]["clhome2"] .'</td><td align="right">'. htmlspecialchars($val["home2telephonenumber"]) .'</td></tr></table>';
			}

			if (!empty($val["mobiletelephonenumber"]))
			{
				$coldata .= '<tr class="calinactivehour"><td><table border="0" cellpadding="3" cellspacing="0" width="100%"><tr><td align="left" width="50%" nowrap>'. $_SWIFT["language"]["clmobile"] .'</td><td align="right">'. htmlspecialchars($val["mobiletelephonenumber"]) .'</td></tr></table>';
			}

			if (!empty($val["pagernumber"]))
			{
				$coldata .= '<tr class="calinactivehour"><td><table border="0" cellpadding="3" cellspacing="0" width="100%"><tr><td align="left" width="50%" nowrap>'. $_SWIFT["language"]["clpager"] .'</td><td align="right">'. htmlspecialchars($val["pagernumber"]) .'</td></tr></table>';
			}

			if (!empty($val["email1address"]))
			{
				$coldata .= '<tr class="calinactivehour"><td><table border="0" cellpadding="3" cellspacing="0" width="100%"><tr><td align="left" width="50%" nowrap>'. $_SWIFT["language"]["clemail"] .'</td><td align="right">'. htmlspecialchars($val["email1address"]) .'</td></tr></table>';
			}

			if (!empty($val["email2address"]))
			{
				$coldata .= '<tr class="calinactivehour"><td><table border="0" cellpadding="3" cellspacing="0" width="100%"><tr><td align="left" width="50%" nowrap>'. $_SWIFT["language"]["clemail"] .'</td><td align="right">'. htmlspecialchars($val["email2address"]) .'</td></tr></table>';
			}

			if (!empty($val["email3address"]))
			{
				$coldata .= '<tr class="calinactivehour"><td><table border="0" cellpadding="3" cellspacing="0" width="100%"><tr><td align="left" width="50%" nowrap>'. $_SWIFT["language"]["clemail"] .'</td><td align="right">'. htmlspecialchars($val["email3address"]) .'</td></tr></table>';
			}

			if (!empty($val["webpage"]))
			{
				$coldata .= '<tr class="calinactivehour"><td><table border="0" cellpadding="3" cellspacing="0" width="100%"><tr><td align="left" width="50%" nowrap>'. $_SWIFT["language"]["clweb"] .'</td><td align="right"><a href="'.$val["webpage"].'" target="_blank">'. htmlspecialchars($val["webpage"]) .'</a></td></tr></table>';
			}
		}


		$coldata .= '</td></tr></table><BR />';

		$_columnstore[$column] .= $coldata;
		$contactcount++;
	}

	$data .= '<table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td align="left" valign="top">';
	// Column #1
	$data .= $_columnstore[0];
	$data .= '&nbsp;</td>';

	if (count($_columnstore[1]))
	{
		$data .= '<td width="4"><img src="'. $_SWIFT["themepath"] .'space.gif" width="4" height="1" /></td><td width="1" valign="top"><div id="contactcolumn1" class="contactsplit"><img src="'. $_SWIFT["themepath"] .'space.gif" width="1" height="1" /></div></td><td width="4"><img src="'. $_SWIFT["themepath"] .'space.gif" width="4" height="1" /></td>';
		$data .= '<td align="left" valign="top">';
		// Column #2
		$data .= $_columnstore[1];
		$data .= '&nbsp;</td>';
	}

	if (count($_columnstore[2]))
	{
		$data .= '<td width="4"><img src="'. $_SWIFT["themepath"] .'space.gif" width="4" height="1" /></td><td width="1" valign="top"><div id="contactcolumn2" class="contactsplit"><img src="'. $_SWIFT["themepath"] .'space.gif" width="1" height="1" /></div></td><td width="4"><img src="'. $_SWIFT["themepath"] .'space.gif" width="4" height="1" /></td>';
		$data .= '<td align="left" valign="top">';
		// Column #3
		$data .= $_columnstore[2];
		$data .= '&nbsp;</td>';
	}

	$data .= '</tr></table>';

	return $data;
}

/**
* Get the Sort Options based on View Type
*/
function getContactGridSort($_viewtype)
{
	if ($_viewtype == CONTACTVIEW_BYCOMPANY)
	{
		return array("companyname", "asc");
	}

	return array("fullname", "asc");

}

/**
* Get Grid Type Fields
*/
function getContactGridFields($_viewtype)
{
	global $_SWIFT;

	$fields = array();

	switch ($_viewtype)
	{
		case CONTACTVIEW_PHONELIST:
			$fields[0]["name"] = "contactimg";
			$fields[0]["title"] = "&nbsp;";
			$fields[0]["width"] = "16";
			$fields[0]["type"] = "custom";

			$fields[1]["name"] = "fullname";
			$fields[1]["title"] = $_SWIFT["language"]["clafullname"];
			$fields[1]["width"] = "";

			$fields[2]["name"] = "businesstelephonenumber";
			$fields[2]["title"] = $_SWIFT["language"]["clabusinessphone"];
			$fields[2]["width"] = "150";
			$fields[2]["align"] = "center";

			$fields[3]["name"] = "businessfaxnumber";
			$fields[3]["title"] = $_SWIFT["language"]["clabusinessfax"];
			$fields[3]["width"] = "150";
			$fields[3]["align"] = "center";

			$fields[4]["name"] = "hometelephonenumber";
			$fields[4]["title"] = $_SWIFT["language"]["clahomephone"];
			$fields[4]["width"] = "150";
			$fields[4]["align"] = "center";

			$fields[5]["name"] = "mobiletelephonenumber";
			$fields[5]["title"] = $_SWIFT["language"]["clamobilephone"];
			$fields[5]["width"] = "150";
			$fields[5]["align"] = "center";
			break;

		case CONTACTVIEW_BYCOMPANY:
			$fields[0]["name"] = "contactimg";
			$fields[0]["title"] = "&nbsp;";
			$fields[0]["width"] = "16";
			$fields[0]["type"] = "custom";

			$fields[1]["name"] = "fullname";
			$fields[1]["title"] = $_SWIFT["language"]["clafullname"];
			$fields[1]["width"] = "";

			$fields[2]["name"] = "jobtitle";
			$fields[2]["title"] = $_SWIFT["language"]["clajobtitle"];
			$fields[2]["width"] = "120";
			$fields[2]["align"] = "center";

			$fields[3]["name"] = "companyname";
			$fields[3]["title"] = $_SWIFT["language"]["clacompany"];
			$fields[3]["width"] = "150";
			$fields[3]["align"] = "center";

			$fields[4]["name"] = "department";
			$fields[4]["title"] = $_SWIFT["language"]["cladepartment"];
			$fields[4]["width"] = "150";
			$fields[4]["align"] = "center";

			$fields[5]["name"] = "businesstelephonenumber";
			$fields[5]["title"] = $_SWIFT["language"]["clabusinessphone"];
			$fields[5]["width"] = "150";
			$fields[5]["align"] = "center";

			$fields[6]["name"] = "businessfaxnumber";
			$fields[6]["title"] = $_SWIFT["language"]["clabusinessfax"];
			$fields[6]["width"] = "150";
			$fields[6]["align"] = "center";
			break;

		case CONTACTVIEW_DETAILLIST:
			$fields[0]["name"] = "contactimg";
			$fields[0]["title"] = "&nbsp;";
			$fields[0]["width"] = "16";
			$fields[0]["type"] = "custom";

			$fields[1]["name"] = "fullname";
			$fields[1]["title"] = $_SWIFT["language"]["clafullname"];
			$fields[1]["width"] = "";

			$fields[2]["name"] = "companyname";
			$fields[2]["title"] = $_SWIFT["language"]["clacompany"];
			$fields[2]["width"] = "150";
			$fields[2]["align"] = "center";

			$fields[3]["name"] = "department";
			$fields[3]["title"] = $_SWIFT["language"]["cladepartment"];
			$fields[3]["width"] = "150";
			$fields[3]["align"] = "center";

			$fields[4]["name"] = "calendarcategoryid";
			$fields[4]["title"] = $_SWIFT["language"]["clacategory"];
			$fields[4]["width"] = "150";
			$fields[4]["align"] = "center";

			$fields[5]["name"] = "businesstelephonenumber";
			$fields[5]["title"] = $_SWIFT["language"]["clabusinessphone"];
			$fields[5]["width"] = "150";
			$fields[5]["align"] = "center";
			break;
	}

	return $fields;
}

/**
* Callback for parsing Contact data
*/
function _contactFields($arg)
{
	global $_SWIFT;
	$record = &$arg[0];
	$fields = &$arg[1];

	if ($record["contacttype"] == SWIFTPUBLIC)
	{
		$record["contactimg"] = '<img src="'. $_SWIFT["themepath"] .'icon_contacts.gif" border="0" align="absmiddle" />';
	} else if ($record["contacttype"] == SWIFTPRIVATE) {
		$record["contactimg"] = '<img src="'. $_SWIFT["themepath"] .'icon_contactprivate.gif" border="0" align="absmiddle" />';
	}

	$record["fullname"] = '<a href="index.php?_m=teamwork&_a=editcontact&contactid='. intval($record["contactid"]) .'">'.htmlspecialchars($record["fullname"]).'</a>';

	$record["calendarcategoryid"] = $_SWIFT["calendarcategorycache"][$record["calendarcategoryid"]]["title"];

	return $record;
}

/**
* Mass Action: Delete Contacts
*/
function _maDeleteContacts()
{
	global $dbCore, $_SWIFT;

	if (_is_array($_POST["itemid"]))
	{
		deleteContacts($_POST["itemid"]);

		$_SWIFT["infomessage"] = $_SWIFT["language"]["contactsdelconfirm"];
	}
}

/**
* Render the Contact Form
*/
function renderContactForm($type)
{
	global $dbCore, $_SWIFT, $module;

	$loadingdata = '<table border="0" cellpadding="3" cellspacing="1" width="100%" class="tborder"><tr><td class="row1"><span class="smalltext"><img src="'. $_SWIFT["themepath"] .'loadingcircle.gif" border="0" align="absmiddle" />&nbsp;'. $_SWIFT["language"]["loading"] .'</span></td></tr></table><BR /> ';

	$_ticketcount = 0;
	$_chatcount = 0;
	$_emaillist = array();
	if (!empty($_POST["email1address"]))
	{
		$_emaillist[] = $_POST["email1address"];
	}
	if (!empty($_POST["email2address"]))
	{
		$_emaillist[] = $_POST["email2address"];
	}
	if (!empty($_POST["email3address"]))
	{
		$_emaillist[] = $_POST["email3address"];
	}

	if (($type == DISPLAY || $type == EDIT) && $module->isRegistered(MODULE_TICKETS) && count($_emaillist))
	{
		$_total = $dbCore->queryFetch("SELECT COUNT(*) AS totalitems FROM `". TABLE_PREFIX ."tickets` WHERE `email` IN (". buildIN($_emaillist) .");");
		$_ticketcount = intval($_total["totalitems"]);
	}
	if (($type == DISPLAY || $type == EDIT) && $module->isRegistered(MODULE_LIVESUPPORT) && count($_emaillist))
	{
		$_total = $dbCore->queryFetch("SELECT COUNT(*) AS totalitems FROM `". TABLE_PREFIX ."chatobjects` WHERE `useremail` IN (". buildIN($_emaillist) .");");
		$_chatcount = intval($_total["totalitems"]);
	}

	printFormStart("cform", "index.php", "POST");
	echo '<table cellspacing="0" cellpadding="0" border="0" width="100%" id="caltableopt"><tr style="height: 1em"><td align="left"><div id="calopt"><ul id="tab">
	
	<li><a class="currenttab" href="#" onClick="this.blur(); switchGridTab(\'clgeneral\', \'contactform\');" id="clgeneral" title="'. $_SWIFT["language"]["general"] .'">'. $_SWIFT["language"]["general"] .'</a></li>
	<li><a href="#" onClick="this.blur(); switchGridTab(\'clbusiness\', \'contactform\');" id="clbusiness" title="'. $_SWIFT["language"]["business"] .'">'. $_SWIFT["language"]["business"] .'</a></li>
	<li><a href="#" onClick="this.blur(); switchGridTab(\'clpersonal\', \'contactform\');" id="clpersonal" title="'. $_SWIFT["language"]["personal"] .'">'. $_SWIFT["language"]["personal"] .'</a></li>
	<li><a href="#" onClick="this.blur(); switchGridTab(\'clmisc\', \'contactform\');" id="clmisc" title="'. $_SWIFT["language"]["misc"] .'">'. $_SWIFT["language"]["misc"] .'</a></li>';

	if (($type == DISPLAY || $type == EDIT) && $module->isRegistered(MODULE_TICKETS))
	{
		echo '<li><a href="#" onClick="this.blur(); switchGridTab(\'cltickets\', \'contactform\');fetchData(\'cltickets\', \'&email[]='. urlencode($_POST["email1address"]) .'&email[]='.urlencode($_POST["email2address"]) .'&email[]='. urlencode($_POST["email3address"]) .'\');" id="cltickets" title="'. sprintf($_SWIFT["language"]["twtickets"], $_ticketcount) .'">'. sprintf($_SWIFT["language"]["twtickets"], $_ticketcount) .'</a></li>';
	}

	if (($type == DISPLAY || $type == EDIT) && $module->isRegistered(MODULE_LIVESUPPORT))
	{
		echo '<li><a href="#" onClick="this.blur(); switchGridTab(\'clchats\', \'contactform\');fetchData(\'clchats\', \'&email[]='. urlencode($_POST["email1address"]) .'&email[]='.urlencode($_POST["email2address"]) .'&email[]='. urlencode($_POST["email3address"]) .'\');" id="clchats" title="'. sprintf($_SWIFT["language"]["twchats"], $_chatcount) .'">'. sprintf($_SWIFT["language"]["twchats"], $_chatcount) .'</a></li>';
	}

	echo '<li><a href="#" onClick="this.blur(); switchGridTab(\'clcustom\', \'contactform\');" id="clcustom" title="'. $_SWIFT["language"]["custom"] .'">'. $_SWIFT["language"]["custom"] .'</a></li>
	</ul></div></td></tr>'.SWIFT_CRLF;

	echo '<tr style="height: 1em"><td align="left">'.SWIFT_CRLF;

	$buttontext = iif($type==INSERT, $_SWIFT["language"]["insert"], $_SWIFT["language"]["update"]);

	// ======= GENERAL TAB =======
	$infolist[] = array($_SWIFT["language"]["clffullname"], iif($type==DISPLAY, $_POST["fullname"], '<input type="text" class="swifttext" name="fullname" value="'. $_POST["fullname"] .'" size="25" />'));

	$contacttype = '<label for="contacttypeyes">'. '<input type="radio" name="contacttype" class="swiftradio" id="contacttypeyes" value="public"'. iif($_POST["contacttype"]==SWIFTPUBLIC||empty($_POST["contacttype"]), " checked") .' />'.$_SWIFT["language"]["public"].'</label>'.SWIFT_CRLF;
	$contacttype .= '<label for="contacttypeno">'. '<input type="radio" name="contacttype" id="contacttypeno" value="private"'. iif($_POST["contacttype"]==SWIFTPRIVATE, " checked") .' />'.$_SWIFT["language"]["private"].'</label>'.SWIFT_CRLF;

	$infolist[] = array($_SWIFT["language"]["clftype"], iif($type==DISPLAY, iif($_POST["contacttype"]==SWIFTPUBLIC, $_SWIFT["language"]["public"], $_SWIFT["language"]["private"]), $contacttype));
	$infolist[] = array($_SWIFT["language"]["clftitle"], iif($type==DISPLAY, $_POST["jobtitle"], '<input type="text" class="swifttext" name="jobtitle" value="'. $_POST["jobtitle"] .'" size="20" />'));
	$infolist[] = array($_SWIFT["language"]["clfinitials"], iif($type==DISPLAY, $_POST["initials"], '<input type="text" class="swifttext" name="initials" value="'. $_POST["initials"] .'" size="10" />'));
	$infolist[] = array($_SWIFT["language"]["clfemail1"], iif($type==DISPLAY, $_POST["email1address"], '<input type="text" class="swifttext" name="email1address" value="'. $_POST["email1address"] .'" size="30" />'));
	$infolist[] = array($_SWIFT["language"]["clfemail2"], iif($type==DISPLAY, $_POST["email2address"], '<input type="text" class="swifttext" name="email2address" value="'. $_POST["email2address"] .'" size="30" />'));
	$infolist[] = array($_SWIFT["language"]["clfemail3"], iif($type==DISPLAY, $_POST["email3address"], '<input type="text" class="swifttext" name="email3address" value="'. $_POST["email3address"] .'" size="30" />'));

	$calendarcategory = '<select name="calendarcategoryid" class="swiftselect">';
	foreach ($_SWIFT["calendarcategorycache"] as $key=>$val)
	{
		if ($val["categorytype"] == CATEGORY_CONTACT)
		{
			$calendarcategory .= '<option value="'. $val["calendarcategoryid"] .'"'. iif($val["calendarcategoryid"]==$_POST["calendarcategoryid"], " selected") .'>'. htmlspecialchars($val["title"]) .'</option>';
		}
	}
	$calendarcategory .= '</select>';
	$infolist[] = array($_SWIFT["language"]["clfcategory"], iif($type==DISPLAY, $_SWIFT["calendarcategorycache"][$_POST["calendarcategoryid"]]["title"], $calendarcategory));

	$infolist[] = array($_SWIFT["language"]["clfcustomerid"], iif($type==DISPLAY, $_POST["customerid"], '<input type="text" class="swifttext" name="customerid" value="'. $_POST["customerid"] .'" size="20" />'));
	$infolist[] = array($_SWIFT["language"]["clfmobilenumber"], iif($type==DISPLAY, $_POST["mobiletelephonenumber"], '<input type="text" class="swifttext" name="mobiletelephonenumber" value="'. $_POST["mobiletelephonenumber"] .'" size="30" />'));

	echo '<div id="tab_clgeneral" style="DISPLAY: block;" class="tabcontent">';
	echo '<table border="0" cellpadding="3" cellspacing="1" width="100%" class="tborder">';
	renderSplitFields($infolist, "row2", "cleanrow");
	if ($type == INSERT || $type == EDIT)
	{
		printSubmitRow($buttontext, "4");
	}
	echo '</table><BR />';
	echo '</div>';

	// ======= BUSINESS TAB =======
	unset($infolist);
	$infolist[] = array($_SWIFT["language"]["clfcompany"], iif($type==DISPLAY, $_POST["companyname"], '<input type="text" class="swifttext" name="companyname" value="'. $_POST["companyname"] .'" size="25" />'));
	$infolist[] = array($_SWIFT["language"]["clfdepartment"], iif($type==DISPLAY, $_POST["department"], '<input type="text" class="swifttext" name="department" value="'. $_POST["department"] .'" size="25" />'));
	$infolist[] = array($_SWIFT["language"]["clfaddress"], iif($type==DISPLAY, nl2br(htmlspecialchars($_POST["businessaddress"])), '<textarea class="swifttextarea" name="businessaddress" cols="30" rows="3">'. htmlspecialchars($_POST["businessaddress"]) .'</textarea>'));
	$infolist[] = array($_SWIFT["language"]["clfofficelocation"], iif($type==DISPLAY, $_POST["officelocation"], '<input type="text" class="swifttext" name="officelocation" value="'. $_POST["officelocation"] .'" size="25" />'));
	$infolist[] = array($_SWIFT["language"]["clftelephone"], iif($type==DISPLAY, $_POST["businesstelephonenumber"], '<input type="text" class="swifttext" name="businesstelephonenumber" value="'. $_POST["businesstelephonenumber"] .'" size="25" />'));
	$infolist[] = array($_SWIFT["language"]["clftelephone2"], iif($type==DISPLAY, $_POST["business2telephonenumber"], '<input type="text" class="swifttext" name="business2telephonenumber" value="'. $_POST["business2telephonenumber"] .'" size="25" />'));
	$infolist[] = array($_SWIFT["language"]["clffax"], iif($type==DISPLAY, $_POST["businessfaxnumber"], '<input type="text" class="swifttext" name="businessfaxnumber" value="'. $_POST["businessfaxnumber"] .'" size="25" />'));
	$infolist[] = array($_SWIFT["language"]["clfwebpage"], iif($type==DISPLAY, $_POST["businesshomepage"], '<input type="text" class="swifttext" name="businesshomepage" value="'. $_POST["businesshomepage"] .'" size="25" />'));

	echo '<div id="tab_clbusiness" style="DISPLAY: none;" class="tabcontent">';
	echo '<table border="0" cellpadding="3" cellspacing="1" width="100%" class="tborder">';
	renderSplitFields($infolist, "row2", "cleanrow");
	if ($type == INSERT || $type == EDIT)
	{
		printSubmitRow($buttontext, "4");
	}
	echo '</table><BR />';
	
	echo '</div>';


	// ======= PERSONAL TAB =======
	unset($infolist);
	$infolist[] = array($_SWIFT["language"]["clfaddress"], iif($type==DISPLAY, nl2br(htmlspecialchars($_POST["homeaddress"])), '<textarea class="swifttextarea" name="homeaddress" cols="30" rows="3">'. htmlspecialchars($_POST["homeaddress"]) .'</textarea>'));
	$infolist[] = array($_SWIFT["language"]["clffax"], iif($type==DISPLAY, $_POST["homefaxnumber"], '<input type="text" class="swifttext" name="homefaxnumber" value="'. $_POST["homefaxnumber"] .'" size="25" />'));
	$infolist[] = array($_SWIFT["language"]["clftelephone"], iif($type==DISPLAY, $_POST["hometelephonenumber"], '<input type="text" class="swifttext" name="hometelephonenumber" value="'. $_POST["hometelephonenumber"] .'" size="25" />'));
	$infolist[] = array($_SWIFT["language"]["clftelephone2"], iif($type==DISPLAY, $_POST["home2telephonenumber"], '<input type="text" class="swifttext" name="home2telephonenumber" value="'. $_POST["home2telephonenumber"] .'" size="25" />'));

if ($type == INSERT)
	{
		if (!empty($_POST["birthdaydateline"]))
		{
			$_birthdaydate = calenderDateToDateline($_POST["birthdaydateline"]);
		}
	} else if ($type == EDIT) {
		if (!empty($_POST["birthdaydateline"]))
		{
			$_birthdaydate = $_POST["birthdaydateline"];
		}
	}

	$infolist[] = array($_SWIFT["language"]["clfbirthday"], iif($type==DISPLAY, iif(!empty($_POST["birthdaydateline"]), generateCalendarTimeline($_POST["birthdaydateline"])), '<input type="text" name="birthdaydateline" id="birthdaydateline" onclick="return showCalendar(\'birthdaydateline\', \''. iif($_SWIFT["settings"]["dt_caltype"]=="us", "%m/%d/%Y", "%d/%m/%Y") .'\');" size="12" readonly="1" value="'. iif(!empty($_birthdaydate), generateCalendarTimeline($_birthdaydate)) .'" class="swifttext"/>&nbsp;<img src="'. $_SWIFT["themepath"] .'calendar.gif" onclick="return showCalendar(\'birthdaydateline\', \''. iif($_SWIFT["settings"]["dt_caltype"]=="us", "%m/%d/%Y", "%d/%m/%Y") .'\');" style="cursor: pointer;" align="absmiddle"/>'));

if ($type == INSERT)
	{
		if (!empty($_POST["birthdaydateline"]))
		{
			$_anniversarydate = calenderDateToDateline($_POST["anniversarydateline"]);
		}
	} else if ($type == EDIT) {
		if (!empty($_POST["anniversarydateline"]))
		{
			$_anniversarydate = $_POST["anniversarydateline"];
		}
	}

	$infolist[] = array($_SWIFT["language"]["clfanniversary"], iif($type==DISPLAY, iif(!empty($_POST["anniversarydateline"]), generateCalendarTimeline($_POST["anniversarydateline"])), '<input type="text" name="anniversarydateline" id="anniversarydateline" onclick="return showCalendar(\'anniversarydateline\', \''. iif($_SWIFT["settings"]["dt_caltype"]=="us", "%m/%d/%Y", "%d/%m/%Y") .'\');" size="12" readonly="1" value="'. iif(!empty($_anniversarydate), generateCalendarTimeline($_anniversarydate)) .'" class="swifttext"/>&nbsp;<img src="'. $_SWIFT["themepath"] .'calendar.gif" onclick="return showCalendar(\'anniversarydateline\', \''. iif($_SWIFT["settings"]["dt_caltype"]=="us", "%m/%d/%Y", "%d/%m/%Y") .'\');" style="cursor: pointer;" align="absmiddle"/>'));

	echo '<div id="tab_clpersonal" style="DISPLAY: none;" class="tabcontent">';
	echo '<table border="0" cellpadding="3" cellspacing="1" width="100%" class="tborder">';
	renderSplitFields($infolist, "row2", "cleanrow");
	if ($type == INSERT || $type == EDIT)
	{
		printSubmitRow($buttontext, "4");
	}
	echo '</table><BR />';
	
	echo '</div>';

	// ======= MISC TAB =======
	unset($infolist);
	$infolist[] = array($_SWIFT["language"]["clfmailingaddress"], iif($type==DISPLAY, nl2br(htmlspecialchars($_POST["mailingaddress"])), '<textarea class="swifttextarea" name="mailingaddress" cols="30" rows="3">'. htmlspecialchars($_POST["mailingaddress"]) .'</textarea>'));
	$infolist[] = array($_SWIFT["language"]["clfotheraddress"], iif($type==DISPLAY, nl2br(htmlspecialchars($_POST["otheraddress"])), '<textarea class="swifttextarea" name="otheraddress" cols="30" rows="3">'. htmlspecialchars($_POST["otheraddress"]) .'</textarea>'));
	$infolist[] = array($_SWIFT["language"]["clfassistant"], iif($type==DISPLAY, $_POST["assistantname"], '<input type="text" class="swifttext" name="assistantname" value="'. $_POST["assistantname"] .'" size="25" />'));
	$infolist[] = array($_SWIFT["language"]["clfassistantphone"], iif($type==DISPLAY, $_POST["assistanttelephonenumber"], '<input type="text" class="swifttext" name="assistanttelephonenumber" value="'. $_POST["assistanttelephonenumber"] .'" size="25" />'));

	$infolist[] = array($_SWIFT["language"]["clfwebpage"], iif($type==DISPLAY, $_POST["webpage"], '<input type="text" class="swifttext" name="webpage" value="'. $_POST["webpage"] .'" size="25" />'));
	$infolist[] = array($_SWIFT["language"]["clfcarphone"], iif($type==DISPLAY, $_POST["cartelephonenumber"], '<input type="text" class="swifttext" name="cartelephonenumber" value="'. $_POST["cartelephonenumber"] .'" size="25" />'));
	$infolist[] = array($_SWIFT["language"]["clfpager"], iif($type==DISPLAY, $_POST["pagernumber"], '<input type="text" class="swifttext" name="pagernumber" value="'. $_POST["pagernumber"] .'" size="25" />'));
	$infolist[] = array($_SWIFT["language"]["clfotherphone"], iif($type==DISPLAY, $_POST["othertelephonenumber"], '<input type="text" class="swifttext" name="othertelephonenumber" value="'. $_POST["othertelephonenumber"] .'" size="25" />'));
	$infolist[] = array($_SWIFT["language"]["clfimaddress"], iif($type==DISPLAY, $_POST["imaddress"], '<input type="text" class="swifttext" name="imaddress" value="'. $_POST["imaddress"] .'" size="25" />'));
	echo '<div id="tab_clmisc" style="DISPLAY: none;" class="tabcontent">';
	echo '<table border="0" cellpadding="3" cellspacing="1" width="100%" class="tborder">';
	renderSplitFields($infolist, "row2", "cleanrow");
	echo '<tr class="descrow">
<td class="descrow" align="left" valign="top" colspan="4">'. $_SWIFT["language"]["clfdescription"] .'</td>
</tr>';
	echo '<tr><td colspan="4" class="cleanrow">'.iif($type==DISPLAY, nl2br(htmlspecialchars($_POST["contents"])), '<textarea style="WIDTH: 98%;" class="swifttextarea" name="body" cols="50" rows="6">'. htmlspecialchars($_POST["contents"]) .'</textarea>') .'</td></tr>';
	if ($type == INSERT || $type == EDIT)
	{
		printSubmitRow($buttontext, "4");
	}
	echo '</table><BR />';
	
	echo '</div>';

	// ======= TICKETS =======
	echo '<div id="tab_cltickets" style="DISPLAY: none;" class="tabcontent">'. $loadingdata .'</div>';

	// ======= CHAT HISTORY =======
	echo '<div id="tab_clchats" style="DISPLAY: none;" class="tabcontent">'. $loadingdata .'</div>';

	// ======= CUSTOM FIELD TAB =======
	echo '<div id="tab_clcustom" style="DISPLAY: none;" class="tabcontent">';
	echo '<table border="0" cellpadding="3" cellspacing="1" width="100%" class="tborder">';

	ob_start();
	renderStaffCustomFields(CUSTOMFIELD_CONTACT, false, iif($type==EDIT||$type==DISPLAY, $_POST["contactid"], 0));
	$contents = ob_get_contents();
	ob_end_clean();
	if (trim($contents) == "")
	{
		echo '<tr><td class="row2">'. $_SWIFT["language"]["nocferror"] .'</td></tr>';
	} else {
		echo $contents;
		if ($type == INSERT || $type == EDIT)
		{
			printSubmitRow($buttontext, "4");
		}
	}

	echo '</table><BR />';
	echo '</div>';

	echo '</td></tr></table>';
	printHiddenField("_m", "teamwork");
	printHiddenField("_a", iif($type==INSERT, "insertcontact", "editcontact"));
	printHiddenField("step", "1");
	printHiddenField("mode", "edit");
	if ($type == EDIT)
	{
		printHiddenField("contactid", $_POST["contactid"]);
	}
	printFormEnd();
}

?>